package com.example.viewdmeo;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Spinner;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class MainActivity extends AppCompatActivity {

    private  String selectColor;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        intiView();
        intiList();
        intiMultiList();
    }

    private void intiView(){
        Spinner colorSpanner=findViewById(R.id.spinner_color);
        //参数2：strings.xml中的数据
        //参数3：Spinner头部布局，当前使用系统布局android.R.layout.simple_spinner_dropdown_item
        //ArrayAdapter adapter=ArrayAdapter.createFromResource(this,R.array.spanner_color_list,
               // android.R.layout.simple_spinner_dropdown_item);

        List<String> list =new ArrayList<>();
        list.add("选择颜色");
        list.add("红色|red");
        list.add("蓝色|blue");
        list.add("绿色|green");
        list.add("粉色|pink");
        //参数2 下拉列表条目  参数3 动态数据
        ArrayAdapter<String> adapter=new ArrayAdapter<>(this, android.R.layout.simple_spinner_dropdown_item,list);
        //给下拉列表设置列表条目布局
        adapter.setDropDownViewResource(
                android.R.layout.simple_spinner_dropdown_item);
        colorSpanner.setAdapter(adapter);
        //设置是否显示控件，对应属性为android:visible
        colorSpanner.setVisibility(View.VISIBLE);
        //监听spanner具体选中了哪个条目
        colorSpanner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            //position 数据array脚标
            public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {
                if (position != 0 || !TextUtils.isEmpty(selectColor)) {
                    //String[] colors = getResources().getStringArray(R.array.spanner_color_list);
                    //Toast.makeText(MainActivity.this,
                      //      "选中了" + getResources().getStringArray(R.array.spanner_color_list)[position],
                        //    Toast.LENGTH_LONG).show();
                    Toast.makeText(MainActivity.this,"选中了："+list.get(position),Toast.LENGTH_SHORT).show();//
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });

    }
    private void intiList(){
        //xml中添加复合控件
        ListView listView=findViewById(R.id.list_view);
        //构造数据
        List<String> list=new ArrayList<>();
        for (int i=0;i<20;i++){
            list.add("联系人 张三："+i);
        }
        //创建列表条目布局
        //构建适配器adapter
        ArrayAdapter<String> adapter=new ArrayAdapter<>(this,R.layout.list_item_text,list);
        //将适配器adapter赋值给ListView
        listView.setAdapter(adapter);
    }
    private void intiMultiList(){
        //xml中添加复合控件
        ListView listView=findViewById(R.id.list_view);
        List<Map<String,Object>> data=new ArrayList<>();
        for (int i=0;i<20;i++){
            Map<String,Object> item=new HashMap<>();
            item.put("item_name","张三"+i);
            item.put("item_icon",R.mipmap.ic_launcher);
            data.add(item);
        }
        //构造条目布局list_item_contact.xml
        //参数4 数据list中map的key对应的值
        //参数5  将参数4中key取出的值赋值给条目布局中的对应控件id
        SimpleAdapter adapter=new SimpleAdapter(this,data,R.layout.list_item_contact,
                new String[]{"item_name","item_icon"},
                new int[]{R.id.item_contact_text,R.id.item_contact_icon});
        //将适配器赋值给复合控件
        listView.setAdapter(adapter);
    }
}